package Impl;

import Util.JDBC;
import dao.userDao;
import entity.User;

import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class userDaoImpl implements userDao {
    adminDaoImpl adminDao=new adminDaoImpl();
    Connection conn=null;
    PreparedStatement ps =null;
    ResultSet rs = null;

    @Override
    public void addUser(User user) {
        String sql = "insert into user (uname,usex,uphone,uemail,upassword,uRegTime,uType,uFace)" +
                "values(?,?,?,?,?,?,?,?)";
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        String pwd="123456";//初始密码为123456
        Object[] params = {user.getUname(),user.getUsex(),user.getUphone(),user.getUemail(),pwd,dateString,0,user.getuFace()};
        adminDao.commonUp(sql,params);
        System.out.println("成功新增用户！");
    }

    public  List<User> find_common_code(ResultSet rs) throws SQLException {
        List<User> users= new ArrayList<>();
        while(rs.next()) {
            User tmp = new User();
            tmp.setUid(rs.getInt("uid"));
            tmp.setUname(rs.getString("uname"));
            tmp.setUemail(rs.getString("uemail"));
            tmp.setUphone(rs.getString("uphone"));
            tmp.setuRegTime(rs.getString("uRegTime"));
            tmp.setuFace(rs.getString("uFace"));
            tmp.setUsex(rs.getInt("usex"));
            users.add(tmp);
        }
        return users;

    }
    @Override
    public List<User> findUserByPages(int pageNow) {
        int pageCount = 5;
        conn = JDBC.getConnection();
        List<User> users = new ArrayList<>();
        String sql = "select * from user limit ?,?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1, (pageNow-1)*pageCount);
            ps.setObject(2, pageCount);
            rs = ps.executeQuery();
            users=find_common_code(rs);
            System.out.println("分页查询成功");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            JDBC.closeAll(conn,ps,rs);
        }
        return users;
    }

    @Override
    public List<User> findAllUser() {
        conn = JDBC.getConnection();
        List<User> users = new ArrayList<>();
        String sql = "select * from user";
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            users=find_common_code(rs);
            System.out.println("查询成功");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            JDBC.closeAll(conn,ps,rs);
        }
        return users;
    }

    @Override
    public void deleteUser(int uid) {
        String sql="delete from user where uid=?";
        adminDao.commonUp(sql,uid);
        System.out.println("删除成功");
    }

    @Override
    public void updateUser(User user) {
        StringBuilder sql =new StringBuilder( "update user set uname=?,usex=?,uphone=?,uemail=?");
        ArrayList<Object> pList=new ArrayList<Object>();
        pList.add(user.getUname());
        pList.add(user.getUsex());
        pList.add(user.getUphone());
        pList.add(user.getUemail());
        if(user.getuFace()!=null){
            sql.append(",uFace=?");
            pList.add(user.getuFace());
        }
        sql.append(" where uid=?");
        pList.add(user.getUid());
        Object[] param=pList.toArray();
        adminDao.commonUp(sql.toString(), param);
    }

    @Override
    public void updateUserPwd(int uid, String pwd) {
        String sql="update user set upassword=? where uid=?";
        Object[] params = {pwd,uid};
        adminDao.commonUp(sql,params);
        System.out.println("修改密码成功！");
    }

    @Override
    public List<User> findUserByCondition(String condition) {
        System.out.println(condition);
        String sql = "select * from user where uphone=? or uemail=? or uname='"+condition+"'";
        System.out.println(sql);
        conn = JDBC.getConnection();
        List<User> users = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,condition);
            ps.setObject(2,condition);
            rs = ps.executeQuery();
            users=find_common_code(rs);
            System.out.println("查询成功");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            JDBC.closeAll(conn,ps,rs);
        }
        return users;
    }

    @Override
    public User findUserByName(String name) {
        String sql = "select * from user where uphone=? or uemail=? ";
        conn=JDBC.getConnection();
        User tmp=new User();
        try {
            ps=conn.prepareStatement(sql);
            ps.setObject(1,name);
            ps.setObject(2,name);
            rs=ps.executeQuery();
            if (rs.next()){
                tmp.setUid(rs.getInt("uid"));
                tmp.setUname(rs.getString("uname"));
                tmp.setUemail(rs.getString("uemail"));
                tmp.setUphone(rs.getString("uphone"));
                tmp.setUpassword(rs.getString("upassword"));
                tmp.setuRegTime(rs.getString("uRegTime"));
                tmp.setuFace(rs.getString("uFace"));
                tmp.setUsex(rs.getInt("usex"));
            }else{
                tmp=null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBC.closeAll(conn,ps,rs);
        }
        return tmp;
    }

    @Override
    public User findUserById(int uid) {
        String sql = "select * from user where uid=?";
        conn=JDBC.getConnection();
        User tmp=new User();
        try {
            ps=conn.prepareStatement(sql);
            ps.setObject(1,uid);
            rs=ps.executeQuery();
            if (rs.next()){
                tmp.setUid(rs.getInt("uid"));
                tmp.setUname(rs.getString("uname"));
                tmp.setUemail(rs.getString("uemail"));
                tmp.setUphone(rs.getString("uphone"));
                tmp.setUpassword(rs.getString("upassword"));
                tmp.setuRegTime(rs.getString("uRegTime"));
                tmp.setuFace(rs.getString("uFace"));
                tmp.setUsex(rs.getInt("usex"));
            }else{
                tmp=null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBC.closeAll(conn,ps,rs);
        }
        return tmp;
    }

    @Override
    public void updateUsertypeByid(int uid,int type) {
        String sql ="update user set uType=? where uid=?";
        Object[] params ={type,uid};
        adminDao.commonUp(sql,params);
        System.out.println("修改用户状态成功");
    }


//    public static void main(String[] args) {
//
//    }
}
